package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.FilterList;
import com.ruoyi.system.domain.MCourse;
import com.ruoyi.system.domain.Student;
import com.ruoyi.system.domain.StudentApp;
import com.ruoyi.system.service.IStudentService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生表Controller
 * 
 * @author ZHANGQIBO
 * @date 2023-12-14
 */
@RestController
@RequestMapping("/system/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询学生表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 查询学生表列表app
     */
    @GetMapping("/studentListapp")
    public AjaxResult studentListapp(HttpServletRequest request)
    {

        List<StudentApp> studentApps = studentService.selectStudentByApp(request);
        return AjaxResult.success(studentApps);
    }

    /**
     * 筛选学生列表
     */
    @PostMapping("/Filterlist")
    public AjaxResult list()
    {
        JSONArray filterList = redisCache.getCacheObject("filterList");
        List<FilterList> filterLists = JSON.parseArray(String.valueOf(filterList), FilterList.class);
        return AjaxResult.success(filterLists);
    }

    /**
     * APP学生个人信息
     */
    @PostMapping("/StudentSingleApp")
    public AjaxResult StudentSingleApp(@RequestBody Student student)
    {
        Student student1 = studentService.selectStudentByIdApp(student.getId());
        return AjaxResult.success(student1);
    }



    /**
     * 前端select
     * 模糊查询学生表列表
     * @return
     */
    @GetMapping("/list/select")
    public TableDataInfo select(Student student) {
        startPage();
        List<Student> list = studentService.select(student);
        return getDataTable(list);
    }


    /**
     * 导出学生表列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:export')")
    @Log(title = "学生表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生表数据");
    }

    /**
     * 获取学生表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生表
     */
    @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学生表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生表
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学生表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生表
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentService.deleteStudentByIds(ids));
    }
}
